package org.dromara.book.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBaseIsbnBo;
import org.dromara.book.domain.vo.SzlyBaseIsbnVo;
import org.dromara.book.domain.SzlyBaseIsbn;
import org.dromara.book.mapper.SzlyBaseIsbnMapper;
import org.dromara.book.service.ISzlyBaseIsbnService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * ISBN库Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class SzlyBaseIsbnServiceImpl implements ISzlyBaseIsbnService {

    private final SzlyBaseIsbnMapper baseMapper;

    /**
     * 查询ISBN库
     */
    @Override
    public SzlyBaseIsbnVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ISBN库列表
     */
    @Override
    public TableDataInfo<SzlyBaseIsbnVo> queryPageList(SzlyBaseIsbnBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyBaseIsbn> lqw = buildQueryWrapper(bo);
        Page<SzlyBaseIsbnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ISBN库列表
     */
    @Override
    public List<SzlyBaseIsbnVo> queryList(SzlyBaseIsbnBo bo) {
        LambdaQueryWrapper<SzlyBaseIsbn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyBaseIsbn> buildQueryWrapper(SzlyBaseIsbnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBaseIsbn> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIsbn13()), SzlyBaseIsbn::getIsbn13, bo.getIsbn13());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyBaseIsbn::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSubName()), SzlyBaseIsbn::getSubName, bo.getSubName());
        lqw.like(StringUtils.isNotBlank(bo.getParatacticName()), SzlyBaseIsbn::getParatacticName, bo.getParatacticName());
        lqw.eq(StringUtils.isNotBlank(bo.getFirstAuthor()), SzlyBaseIsbn::getFirstAuthor, bo.getFirstAuthor());
        lqw.eq(StringUtils.isNotBlank(bo.getOtherAuthor()), SzlyBaseIsbn::getOtherAuthor, bo.getOtherAuthor());
        lqw.like(StringUtils.isNotBlank(bo.getSeriesName()), SzlyBaseIsbn::getSeriesName, bo.getSeriesName());
        lqw.eq(StringUtils.isNotBlank(bo.getDistinctionNumber()), SzlyBaseIsbn::getDistinctionNumber, bo.getDistinctionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCallNumber()), SzlyBaseIsbn::getCallNumber, bo.getCallNumber());
        lqw.eq(bo.getPrice() != null, SzlyBaseIsbn::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getPublishCompany()), SzlyBaseIsbn::getPublishCompany, bo.getPublishCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getPublishPlace()), SzlyBaseIsbn::getPublishPlace, bo.getPublishPlace());
        lqw.eq(StringUtils.isNotBlank(bo.getPublishDept()), SzlyBaseIsbn::getPublishDept, bo.getPublishDept());
        lqw.eq(bo.getPublishDate() != null, SzlyBaseIsbn::getPublishDate, bo.getPublishDate());
        lqw.eq(StringUtils.isNotBlank(bo.getLiteratureType()), SzlyBaseIsbn::getLiteratureType, bo.getLiteratureType());
        lqw.eq(bo.getPagesNum() != null, SzlyBaseIsbn::getPagesNum, bo.getPagesNum());
        lqw.eq(StringUtils.isNotBlank(bo.getBind()), SzlyBaseIsbn::getBind, bo.getBind());
        lqw.eq(StringUtils.isNotBlank(bo.getBookVersion()), SzlyBaseIsbn::getBookVersion, bo.getBookVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getBookFormat()), SzlyBaseIsbn::getBookFormat, bo.getBookFormat());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), SzlyBaseIsbn::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguageCode()), SzlyBaseIsbn::getLanguageCode, bo.getLanguageCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorNumber()), SzlyBaseIsbn::getAuthorNumber, bo.getAuthorNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBookIntroduction()), SzlyBaseIsbn::getBookIntroduction, bo.getBookIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorIntroduction()), SzlyBaseIsbn::getAuthorIntroduction, bo.getAuthorIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverImage()), SzlyBaseIsbn::getCoverImage, bo.getCoverImage());
        lqw.like(StringUtils.isNotBlank(bo.getDivisionName()), SzlyBaseIsbn::getDivisionName, bo.getDivisionName());
        lqw.eq(StringUtils.isNotBlank(bo.getDivisionNumber()), SzlyBaseIsbn::getDivisionNumber, bo.getDivisionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIsbn10()), SzlyBaseIsbn::getIsbn10, bo.getIsbn10());
        lqw.eq(StringUtils.isNotBlank(bo.getCataloguePeople()), SzlyBaseIsbn::getCataloguePeople, bo.getCataloguePeople());
        lqw.eq(bo.getCatalogueTime() != null, SzlyBaseIsbn::getCatalogueTime, bo.getCatalogueTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSeriesAuthor()), SzlyBaseIsbn::getSeriesAuthor, bo.getSeriesAuthor());
        lqw.eq(StringUtils.isNotBlank(bo.getTypeCode()), SzlyBaseIsbn::getTypeCode, bo.getTypeCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFindBookId()), SzlyBaseIsbn::getFindBookId, bo.getFindBookId());
        lqw.eq(StringUtils.isNotBlank(bo.getGeneralNotes()), SzlyBaseIsbn::getGeneralNotes, bo.getGeneralNotes());
        lqw.eq(StringUtils.isNotBlank(bo.getCip()), SzlyBaseIsbn::getCip, bo.getCip());
        lqw.eq(bo.getWordsNum() != null, SzlyBaseIsbn::getWordsNum, bo.getWordsNum());
        return lqw;
    }

    /**
     * 新增ISBN库
     */
    @Override
    public SzlyBaseIsbnBo insertByBo(SzlyBaseIsbnBo bo) {
        SzlyBaseIsbn add = MapstructUtils.convert(bo, SzlyBaseIsbn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return bo;
    }

    /**
     * 修改ISBN库
     */
    @Override
    public SzlyBaseIsbnBo updateByBo(SzlyBaseIsbnBo bo) {
        SzlyBaseIsbn update = MapstructUtils.convert(bo, SzlyBaseIsbn.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return bo;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBaseIsbn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public List<SzlyBaseIsbnVo> queryByIsbn(String isbn) {
        LambdaQueryWrapper<SzlyBaseIsbn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SzlyBaseIsbn::getIsbn13,isbn).or().eq(SzlyBaseIsbn::getIsbn10,isbn);
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 批量删除ISBN库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
